package com.example.composedialogdemo

import android.app.Activity
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.composedialogdemo.dialog.BottomSheetDialog
import com.example.composedialogdemo.dialog.CustomStyledDialog
import com.example.composedialogdemo.dialog.DatePickerDialog
import com.example.composedialogdemo.dialog.FullScreenDialog
import com.example.composedialogdemo.dialog.IconSimpleDialog
import com.example.composedialogdemo.dialog.InputDialogExample
import com.example.composedialogdemo.dialog.ListSelectionDialog
import com.example.composedialogdemo.dialog.LoadingDialog
import com.example.composedialogdemo.dialog.LocationDialog
import com.example.composedialogdemo.dialog.MultiChoiceDialog
import com.example.composedialogdemo.dialog.ProgressDialog
import com.example.composedialogdemo.dialog.RechargeTipsDialog
import com.example.composedialogdemo.dialog.SampleDialog
import com.example.composedialogdemo.dialog.SingleChoiceDialog
import com.example.composedialogdemo.dialog.TimePickerDialogSample
import com.example.composedialogdemo.ui.theme.ComposeDialofDemoTheme
import kotlinx.coroutines.delay
import java.time.LocalDate
import java.time.LocalTime

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            ComposeDialofDemoTheme {
                var showBottomSheet by remember { mutableStateOf(false) }
                var showLoadingDialog by remember { mutableStateOf(false) }
                var showLocationDialog by remember { mutableStateOf(false) }
                var showIconDialog by remember { mutableStateOf(false) }
                var showInputDialog by remember { mutableStateOf(false) }
                var inputText by remember { mutableStateOf("") }
                var showProgressDialog by remember { mutableStateOf(false) }
                var progress by remember { mutableFloatStateOf(0.3f) }
                var showFullDialog by remember { mutableStateOf(false) }
                var showDatePickerDialog by remember { mutableStateOf(false) }
                var selectedDate by remember { mutableStateOf(LocalDate.now()) }
                var showTimePickerDialog by remember { mutableStateOf(false) }
                var selectedTime by remember { mutableStateOf(LocalTime.now()) }
                var showSampleDialog by remember { mutableStateOf(false) }
                var showCustomSampleDialog by remember { mutableStateOf(false) }
                var showListDialog by remember { mutableStateOf(false) }
                var showMultiChoiceDialog by remember { mutableStateOf(false) }
                val items = listOf("选项1", "选项2", "选项3", "选项4")
                val selectedOptions = remember { mutableStateListOf<Int>() }
                var showSingleChoiceDialog by remember { mutableStateOf(false) }
                var selectedOption by remember { mutableIntStateOf(0) }
                var showRechargeTipsDialog by remember { mutableStateOf(false) }
                Column(
                    modifier = Modifier
                        .fillMaxSize()
                        .padding(6.dp)
                        .verticalScroll(rememberScrollState()),
                    verticalArrangement = Arrangement.spacedBy(6.dp)
                ) {

                    Button(onClick = { showBottomSheet = true }) {
                        Text("显示底部对话框")
                    }
                    Button(onClick = { showLoadingDialog = true }) {
                        Text("显示Loading对话框")
                    }
                    Button(onClick = { showLocationDialog = true }) {
                        Text("显示定位对话框")
                    }
                    Button(onClick = { showIconDialog = true }) {
                        Text("显示图标样式对话框")
                    }
                    Button(onClick = { showInputDialog = true }) {
                        Text("显示输入框样式对话框")
                    }
                    Button(onClick = { showProgressDialog = true }) {
                        Text("显示进度条样式对话框")
                    }
                    Button(onClick = { showFullDialog = true }) {
                        Text("显示全屏样式对话框")
                    }
                    Button(onClick = { showDatePickerDialog = true }) {
                        Text("显示日期样式对话框")
                    }
                    Button(onClick = { showTimePickerDialog = true }) {
                        Text("显示时间选择器对话框")
                    }
                    Button(onClick = { showSampleDialog = true }) {
                        Text("显示简单的对话框")
                    }
                    Button(onClick = { showCustomSampleDialog = true }) {
                        Text("显示自定义样式的对话框")
                    }
                    Button(onClick = { showListDialog = true }) {
                        Text("显示列表样式的对话框")
                    }
                    Button(onClick = { showMultiChoiceDialog = true }) {
                        Text("显示多选列表样式的对话框")
                    }
                    Button(onClick = { showSingleChoiceDialog = true }) {
                        Text("显示单选列表样式的对话框")
                    }
                    Button(onClick = { showRechargeTipsDialog = true }) {
                        Text("显示游玩时长充值提示对话框")
                    }
                    Text("输入内容: $inputText")
                    Text("单选结果: $selectedOption")
                    Text("多选结果: ${selectedOptions.joinToString()}")
                    Text("选择日期: $selectedDate")
                    Text("选择时间: $selectedTime")

                    BottomSheetDialog(
                        showDialog = showBottomSheet,
                        onDismiss = { showBottomSheet = false })

                    LoadingDialog(
                        showDialog = showLoadingDialog,
                        onDismissRequest = { showLoadingDialog = false }
                    )
                    LocationDialog(
                        showDialog = showLocationDialog,
                        onDismiss = { showLocationDialog = false },
                        onConfirm = { showLocationDialog = false }
                    )
                    IconSimpleDialog(
                        showDialog = showIconDialog,
                        onDismiss = { showIconDialog = false }
                    )
                    InputDialogExample(
                        showDialog = showInputDialog,
                        onDismiss = { showIconDialog = false },
                        onConfirm = { text ->
                            inputText = text
                            showInputDialog = false
                        }
                    )
                    LaunchedEffect(showProgressDialog) {
                        if (showProgressDialog) {
                            while (progress < 1f) {
                                delay(100)
                                progress += 0.05f
                            }
                            showProgressDialog = false
                            progress = 0f
                        }
                    }
                    ProgressDialog(
                        showDialog = showProgressDialog,
                        progress = progress
                    )
                    FullScreenDialog(
                        showDialog = showFullDialog,
                        onDismiss = { showFullDialog = false }
                    )
                    DatePickerDialog(
                        showDialog = showDatePickerDialog,
                        onDismiss = { showDatePickerDialog = false },
                        onDateSelected = { date ->
                            selectedDate = date
                            showDatePickerDialog = false
                        }
                    )
                    TimePickerDialogSample(
                        showDialog = showTimePickerDialog,
                        onDismiss = { showTimePickerDialog = false },
                        onTimeSelected = { time ->
                            selectedTime = time
                            showTimePickerDialog = false
                        }
                    )
                    SampleDialog(
                        showDialog = showSampleDialog,
                        onDismiss = { showSampleDialog = false },
                        onConfirm = { showSampleDialog = false }
                    )
                    CustomStyledDialog(
                        showDialog = showCustomSampleDialog,
                        onDismiss = { showCustomSampleDialog = false },
                    )
                    ListSelectionDialog(
                        showDialog = showListDialog,
                        onDismiss = { showListDialog = false },
                        title = "请选择一个选项",
                        items = items,
                        onItemSelected = { index ->
                            println("选择了: ${items[index]}")
                        }
                    )
                    MultiChoiceDialog(
                        showDialog = showMultiChoiceDialog,
                        onDismiss = { showMultiChoiceDialog = false },
                        onConfirm = { indices ->
                            selectedOptions.clear()
                            selectedOptions.addAll(indices)
                            showMultiChoiceDialog = false
                        }
                    )
                    SingleChoiceDialog(
                        showDialog = showSingleChoiceDialog,
                        onDismiss = { showSingleChoiceDialog = false },
                        onConfirm = { index ->
                            selectedOption = index
                            showSingleChoiceDialog = false
                        }
                    )
                    RechargeTipsDialogWrapper(
                        isShowing = showRechargeTipsDialog,
                        onDismiss = {showRechargeTipsDialog = false }// 关闭弹框时更新状态
                    )
                }

            }
        }
    }

        @Composable
        fun RechargeTipsDialogWrapper(
            isShowing: Boolean,
            onDismiss: () -> Unit
        ) {
            if (isShowing) {
                RechargeTipsDialog(
                    onDismiss = onDismiss,
                    onRechargeClick = {
                        onDismiss()
                    }
                )
            }
        }

    @Preview
    @Composable
    fun RechargeTipsDialogPreview() {
        MaterialTheme {
            Surface {
                RechargeTipsDialog(
                    onDismiss = {},
                    onRechargeClick = {}
                )
            }
        }
    }
}

